﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Models;
using RedSand.Web.Mvc;
using RedSand.Web.Mvc.Controls;
using System.Text.RegularExpressions;

namespace RedSand.Site.Controllers
{
    public class ActionController : BaseController
    {
        public ActionResult Manager()
        {
            var list = RsDataManager.GetList<RoleInfo>("RoleGetAll");
            if (list != null)
            {
                ViewBag.RoleList = new string[list.Count];
                Regex re = new Regex(@"\[" + LanguageCode + @":(?<word>[^\]]+)\]");
                for (int i = 0; i < list.Count; i++)
                {
                    ViewBag.RoleList[i] = re.Match(list[i].Name).Groups["word"].Value;
                }
            }
            return View(ActionList.Items);
        }

        public ActionResult Menu()
        {
            return PartialView();
        }

        public ActionResult Save(string[] RoleIDs)
        {
            int count = 0;
            for(int i=0;i<ActionList.Items.Count;i++)
            {
                if (!RsDataManager.Save("ActionSave", new { Link = ActionList.Items[i].Link, RoleIDs = RoleIDs[i], ActionID = ActionList.Items[i].ActionID }))
                {
                    count++;
                }
            }
            return count == 0 ? AjaxMessage(Message.UpdateSuccess) : AjaxMessage(string.Format(Translate("CannotUpdateAnyRecords", "Message"), count));
        }

        public ActionResult GetRoleCheckList(string Link)
        {
            return PartialView("RoleCheckList", RsDataManager.GetList<RoleCheckListModel>("RoleGetRoleForAction", new { Link = Link }));
        }
    }
}
